﻿using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Microsoft.Practices.Unity;
using NUnit.Framework;
using ReactiveGraph.Core;
using ReactiveGraph.Object.Configuration.Containers;

namespace ReactiveGraph.Uat.ResolvableValueUat
{
    [TestFixture]
    public class ResolvableValueTests
    {
        private UnityContainer _container;

        [SetUp]
        public void Setup()
        {
            _container = new UnityContainer();
            _container.AddExtension(new UnityObjectExtension());
            
        }

        [Test]
        public void CanSetValue()
        {
            var resolvableValue = _container.Resolve<ResolvableValue<int>>();
            resolvableValue.Current.Value = 42;
            resolvableValue.Current.Value.Should().Be(42);
        }

        [Test]
        public void SettingValueTriggers()
        {
            var resolvableValue = _container.Resolve<ResolvableValue<int>>();
            var list = new List<IProperty>();
            resolvableValue.PropertyChanged().Do(list.Add).Subscribe();
            resolvableValue.Current.Value = 42;

            list.Should().HaveCount(1);
        }

        [Test]
        public void SetSecondaryValue()
        {
            var resolvableValue = _container.Resolve<ResolvableValue<int>>();
            resolvableValue.Current.SetSecondaryValue("loaded",42);
            var list = new List<IProperty>();
            resolvableValue.PropertyChanged().Do(list.Add).Subscribe();
            resolvableValue.Current.Value.Should().Be(42);
            resolvableValue.Current.SetSecondaryValue("automatic", 40);
            resolvableValue.Current.Value.Should().Be(42);
            list.Should().HaveCount(0);
            resolvableValue.Current.ClearSecondaryValue("loaded");
            resolvableValue.Current.Value.Should().Be(40);
            list.Should().HaveCount(1);
        }

        [Test]
        public void SettingValueResetsValidationState()
        {
            var resolvableValue = _container.Resolve<ResolvableValue<int>>();
            resolvableValue.ValidationState.Value = new ValidationState(){IsError = true};
         
            resolvableValue.Behaviours.Subscribe();

            resolvableValue.Current.Value = 43;
            resolvableValue.ValidationState.Value.IsError.Should().BeFalse();
        }

    }
}